package com.weilaidb.administrator.eg1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.app.Activity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


//public class MainActivity extends AppCompatActivity {
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
////        setContentView(R.layout.activity_main);
//        setContentView(R.layout.main);
//    }
//}

public class ToDoList extends Activity {

    static final private int ADD_NEW_TODO = Menu.FIRST;
    static final private int REMOVE_TODO = Menu.FIRST + 1;

    private boolean addingNew = false;
    private ArrayList<String> todoItems;
    private ListView myListView;
    private EditText myEditText;
    private ArrayAdapter<String> aa;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate your view
        setContentView(R.layout.activity_main);

        // Get references to UI widgets
        myListView = (ListView)findViewById(R.id.myListView);
        myEditText = (EditText)findViewById(R.id.myEditText);

        myEditText.setText("nihaoma");

        todoItems = new ArrayList<String>();
        todoItems.add("niahoma");
        for (int i =0; i < 100;i++)
            todoItems.add("so nice " + i);


        int resID = R.layout.todolist_item;
        aa = new ArrayAdapter<String>(this, resID, todoItems);
        Log.e("SIZE:", "onCreate: todoItems size:" + todoItems.size());
        myListView.setAdapter(aa);

        myEditText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.e("Key", "onKey: " + keyCode + ",action:" + event.getAction());
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                    if (keyCode == KeyEvent.KEYCODE_ENTER) {
                        todoItems.add(0, myEditText.getText().toString());
                        myEditText.setText("");
                        aa.notifyDataSetChanged();
//                        cancelAdd();
                        return true;
                    }
                return false;
            }
        });

        registerForContextMenu(myListView);
    }

//    @Override
//    public  boolean onCreateOptionMenu(Menu menu)
//    {
//        menu.add(0,1,1,"当前位置");
//        menu.add(0,2,2,"地图切换");
//        return  super.onCreateOptionsMenu(menu);
//    }

//    @Override  
//    public boolean onCreateOptionsMenu(Menu menu) 
//    { 
//        // TODO Auto-generated method stub  
//        menu.add(0, 1, 1, "当前位置");  
//        menu.add(0, 2, 2, "地图切换");  
//        if(IsStart == STOPING)  
//            menu.add(0, 3, 3, "开始记录");   
//        else  
//            menu.add(0, 3, 3, "停止记录");   
//        menu.add(0,4,4,"清除路径");  
//        return super.onCreateOptionsMenu(menu);  
//    }  

//    @Override
//    public  boolean opPrepareOptionMenu(Menu menu)
//    {
//        super.onPrepareOptionsMenu(menu);
//        menu.clear();
//        menu.add(0,1,1,"当前位置");
//        menu.add(0,2,2,"地图切换");
//        return  super.onPrepareOptionsMenu(menu);
//    }
////      
//    @Override  
//    public boolean onPrepareOptionsMenu(Menu menu) 
//    {  
//        // TODO Auto-generated method stub  
//        super.onPrepareOptionsMenu(menu);  
//        menu.clear();  
//        menu.add(0, 1, 1, "当前位置");  
//        menu.add(0, 2, 2, "地图切换");  
//        if(IsStart == STOPING)  
//            menu.add(0, 3, 3, "开始记录");   
//        else  
//            menu.add(0, 3, 3, "停止记录");   
//        menu.add(0,4,4,"清除路径");  
//        return super.onPrepareOptionsMenu(menu);  
//    }
//

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_main, menu);
//        return true;
//    }
//


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        Log.e("menu", "onCreateOptionsMenu: " );
        // Create and add new menu items.
        MenuItem itemAdd = menu.add(0, ADD_NEW_TODO, Menu.NONE,
                R.string.add_new);
        MenuItem itemRem = menu.add(0, REMOVE_TODO, Menu.NONE,
                R.string.remove);

        // Assign icons
        itemAdd.setIcon(R.drawable.add_new_item);
        itemRem.setIcon(R.drawable.remove_item);

        // Allocate shortcuts to each of them.
        itemAdd.setShortcut('0', 'a');
        itemRem.setShortcut('1', 'r');

        return true;
    }
//
//    @Override
//    public boolean onPrepareOptionsMenu(Menu menu) {
//        super.onPrepareOptionsMenu(menu);
//
//        int idx = myListView.getSelectedItemPosition();
//
//        String removeTitle = getString(addingNew ?
//                R.string.cancel : R.string.remove);
//
//        MenuItem removeItem = menu.findItem(REMOVE_TODO);
//        removeItem.setTitle(removeTitle);
//        removeItem.setVisible(addingNew || idx > -1);
//
//        return true;
//    }

    @Override
    public void onCreateContextMenu(ContextMenu menu,
                                    View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.setHeaderTitle("Selected To Do Item");
        menu.add(0, REMOVE_TODO, Menu.NONE, R.string.remove);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        int index = myListView.getSelectedItemPosition();

        switch (item.getItemId()) {
            case (REMOVE_TODO): {
                if (addingNew) {
                    cancelAdd();
                }
                else {
                    removeItem(index);
                }
                return true;
            }
            case (ADD_NEW_TODO): {
                addNewItem();
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);
        switch (item.getItemId()) {
            case (REMOVE_TODO): {
                AdapterView.AdapterContextMenuInfo menuInfo;
                menuInfo =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
                int index = menuInfo.position;

                removeItem(index);
                return true;
            }
        }
        return false;
    }

    private void cancelAdd() {
        addingNew = false;
        myEditText.setVisibility(View.GONE);
    }

    private void addNewItem() {
        addingNew = true;
        myEditText.setVisibility(View.VISIBLE);
        myEditText.requestFocus();
    }

    private void removeItem(int _index) {
        try {
            todoItems.remove(_index);
            aa.notifyDataSetChanged();
        }catch (Exception e) {}


    }
}